package ranveterinaria.servlet;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ranveterinaria.excepcion.DAOExcepcion;
import ranveterinaria.excepcion.DatoIncorrectoExcepcion;
import ranveterinaria.modelo.DoctorModelo;
import ranveterinaria.negocio.GestionDoctor;

/**
 * Servlet implementation class DoctorNuevo
 */
@WebServlet("/DoctorNuevo")
public class DoctorNuevo extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DoctorNuevo() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		GestionDoctor negocio = new GestionDoctor();

		try {			
			DoctorModelo p=new DoctorModelo();	
			p.setNombreDoctor(request.getParameter("txtNombre") );
			p.setApePatDoctor(request.getParameter("txtApePaterno"));
			p.setApeMatDoctor (request.getParameter("txtApeMaterno"));
			p.setEmailDoctor(request.getParameter("txtEmail"));
			p.setTlfDoctor(request.getParameter("txtTelefono"));
			String[] strchkAdministrador= request.getParameterValues("chkAdministrador");
			if (strchkAdministrador == null){
				p.setBolAdminDoctor( false );
			}
			else {
				p.setBolAdminDoctor(true ) ;
			} 

			negocio.insertar(p);
			request.setAttribute("MENSAJEEXITO", "Doctor registrado exitosamente");
			RequestDispatcher rd = request.getRequestDispatcher("Formularios/Doctor.jsp");
			rd.forward(request, response);	

		} catch (DAOExcepcion | DatoIncorrectoExcepcion e) {
			request.setAttribute("MENSAJEERROR", e.getMessage());
			RequestDispatcher rd = request.getRequestDispatcher("Formularios/Doctor.jsp");
			rd.forward(request, response);	
		}
	}

}
